
import axios from 'axios'
import qs from 'qs'

const baseurl = process.env.API_ROOT;
// 创建axios实例
const service = axios.create({
  baseURL: baseurl, // api的base_url
  timeout: 15000 // 请求超时时间
})

// request拦截器
service.interceptors.request.use(config => {
  if(config.method === 'post'){
    config.data = qs.stringify(config.data);
    // console.log(config.data)
  }
  return config
}, error => {
  // Do something with request error
  console.log(error) // for debug
  Promise.reject(error)
})

// respone拦截器
service.interceptors.response.use(
  response => {
    const res = response.data
    if (res.code === 405) {
      // to re-login
      sessionStorage.removeItem('token')
      this.$router.push('/login');
    }
    return response.data;
  },
  error => {
    console.log('err' + error) // for debug
    return Promise.reject(error)
  }
)

export default service